using System;
using System.Reflection;
using System.Collections;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.ManagedCode;

namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for ManagedCodeUtil.
	/// </summary>
	class ManagedCodeUtil
	{
		
		public static void createModel( Form form )
		{
			if( form.ManagedCodeModel == null )
			{
				ManagedAssembly managedAssembly = form.Manifest.ManagedAssembly;
				form.ManagedCodeModel = managedAssembly.RootAssembly.CreateInstance( managedAssembly.ModelType.FullName );
			}
		}

		public static MethodInfo findMethod( object model, string name )
		{
			foreach( MethodInfo method in model.GetType().GetMethods() )
			{
				if( method.Name.Equals( name ) )
				{
					return method;
				}
			}
			return null;
		}

		public static MethodInfo findMethod( object model, Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventType eventType )
		{
			foreach( MethodInfo method in model.GetType().GetMethods() )
			{
				foreach( Attribute attr in method.GetCustomAttributes(true) )
				{
					if( attr is Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute )
					{
						Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute eventAttr = (Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute) attr;
						if( eventAttr.EventType == eventType )
						{
							return method;
						}
					}
				}
			}
			return null;
		}

		public static MethodInfo findMethod( object model, Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventType eventType, string matchPath )
		{
			foreach( MethodInfo method in model.GetType().GetMethods() )
			{
				foreach( Attribute attr in method.GetCustomAttributes(true) )
				{
					if( attr is Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute )
					{
						Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute eventAttr = (Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute) attr;
						if( eventAttr.EventType == eventType && eventAttr.MatchPath != null && eventAttr.MatchPath.Equals( matchPath ) )
						{
							return method;
						}
					}
				}
			}
			return null;
		}

		public static ArrayList findOnValidateMethods( object model )
		{
			ArrayList onValidateMethods = new ArrayList();
			foreach( MethodInfo method in model.GetType().GetMethods() )
			{
				foreach( Attribute attr in method.GetCustomAttributes(true) )
				{
					if( attr is Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute )
					{
						Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute eventAttr = (Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute) attr;
						if( eventAttr.EventType == Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventType.OnValidate )
						{
							ManagedCodeMethod managedMethod = new ManagedCodeMethod();
							managedMethod.EventAttr = eventAttr;
							managedMethod.Method = method;
							onValidateMethods.Add( managedMethod );
						}
					}
				}
			}
			return onValidateMethods;
		}

	}
}
